package qrcode

import (
	"encoding/base64"
	"fmt"

	"github.com/skip2/go-qrcode"
)

// QRCode 提供二维码生成功能
type QRCode struct {
	Content string
	Size    int
}

// NewQRCode 创建一个新的二维码实例
func NewQRCode(content string, size int) *QRCode {
	if size <= 0 {
		size = 256
	}
	return &QRCode{
		Content: content,
		Size:    size,
	}
}

// Generate 生成二维码并保存到指定路径
func (q *QRCode) Generate(filePath string) error {
	return qrcode.WriteFile(q.Content, qrcode.Medium, q.Size, filePath)
}

// GenerateBase64 生成二维码并返回Base64编码
func (q *QRCode) GenerateBase64() (string, error) {
	png, err := qrcode.Encode(q.Content, qrcode.Medium, q.Size)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(png)), nil
}
